package com.wbl.train.batch.config;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * Author: 王博纶
 * Date: 2024/2/8 16:47
 * FileName: ScheduleConfig
 * Description: 集成quartz的配置类
 */
@Configuration
public class ScheduleConfig {

    @Resource
    private JobFactory jobFactory;

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(@Qualifier("dataSource") DataSource dataSource) throws IOException {
        // 创建一个定时任务的工厂对象，并交给spring进行管理
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        // 设置quartz的数据连接元
        factory.setDataSource(dataSource);
        // 设置定时任务执行的工厂
        factory.setJobFactory(jobFactory);
        // 程序启动好2秒后执行quartz
        factory.setStartupDelay(2);
        return factory;
    }
}
